package com.xssn.fjnd.content.api;


import com.aliyuncs.exceptions.ClientException;
import com.xssn.fjnd.content.req.VideoREQ;
import com.xssn.fjnd.content.service.IVideoService;
import com.xssn.fjnd.entities.Video;
import com.xssn.util.base.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Api(value = "前台阿里云视频点播微服务", description = "前台阿里云视频点播微服务")
@RestController
@Slf4j
@RequestMapping("/api/video")
public class ApiVideoController {

    @Autowired
    private IVideoService videoService;


    @ApiOperation("根据视频标题和视频类型查询视频分页列表接口")
    @PostMapping("/search") // /content/api/video/search
    public Result search(@RequestBody VideoREQ req) {
        return videoService.queryPage(req);
    }

    @ApiOperation("根据视频播放量查询前四的视频")
    @GetMapping("/searchVideoByType/{type}") // /content/api/video/search
    public Result searchVideoByType(
            @ApiParam(value = "视频类型（0: 纪录片, 1：日常表演，2：训练，3：教学视频）", required = true)
            @PathVariable("type") Integer type) {
        return videoService.searchVideoByType(type);
    }



    @ApiOperation(value = "根据ID查询视频详情")
    @GetMapping("getVideoInfo/{id}")
    public Result getVideInfoById(@PathVariable String id) throws ClientException {

        Video video = videoService.getById(id);
        String playInfo = videoService.getPlayInfo(video.getVideoSourceId());
        video.setTemporaryUrl(playInfo);
        video.setTemporaryAuth(videoService.getPlayAuth(video.getVideoSourceId()));

        return Result.ok(video);
    }


    @ApiOperation("更新视频浏览次数")
    @ApiImplicitParam(name = "id", value = "视频ID", required = true)
    @PutMapping("/playCount/{id}") // /api/video/viewCount/{id}
    public Result updatePlayCount(@PathVariable("id") String id) {
        return videoService.updatePlayCount(id);
    }


}
